﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using pswb.Classes;
using pswb.Forms.Wizard;
using WeifenLuo.WinFormsUI.Docking;

namespace pswb.Forms.ProcessEdit
{
    public partial class frmProcessEditor : Form
    {        
        private DeserializeDockContent myDC;
        private int ProcessNumber = 1;

        public frmProcessEditor()
        {
            InitializeComponent();
        }

        private void frmProcessEditor_Load(object sender, EventArgs e)
        {
            dockPanel1.ShowDocumentIcon = false;
            if (Workspace.WorkspaceExists(Workspaces.PROCESS_EDITOR))
            {
                //load the saved workspace
                myDC = new DeserializeDockContent(GetFromConfigFile);
                dockPanel1.LoadFromXml(Workspace.WorkspaceLocation(Workspaces.PROCESS_EDITOR), myDC);
            }
            else
            {
                DefaultWorkspace();
            }

            dockPanel1.ActiveContentChanged += new EventHandler(dockPanel1_ActiveContentChanged);
        }

        void dockPanel1_ActiveContentChanged(object sender, EventArgs e)
        {
            DockPanel dock = (DockPanel)sender;
            if(dock.ActiveDocument != null)
                saveToolStripButton.Enabled = (dock.ActiveDocument.GetType() == typeof(frmDocument));
        }

        private void DefaultWorkspace()
        {
            frmProcessFields toolWinFileds = new frmProcessFields();
            toolWinFileds.ShowHint = DockState.DockLeft;
            toolWinFileds.HideOnClose = false;
            toolWinFileds.DockAreas = Workspace.AllowedDockAreas(WindowTypes.TOOLWINDOW);
            toolWinFileds.Show(dockPanel1);

            //start page
            frmProcessStartPage frmStartPage = new frmProcessStartPage();
            frmStartPage.Text = "Start";
            frmStartPage.AllowDrop = true;
            frmStartPage.ShowHint = DockState.Document;
            frmStartPage.DockAreas = Workspace.AllowedDockAreas(WindowTypes.DOCUMENT);
            frmStartPage.onProcessTypeSelected += new EventHandler(frmStartPage_onProcessTypeSelected);
            frmStartPage.Show(dockPanel1);

            //properties window
            frmProcessProperty frmProp = new frmProcessProperty();
            frmProp.ShowHint = DockState.DockRight;
            frmProp.HideOnClose = false;
            frmProp.DockAreas = Workspace.AllowedDockAreas(WindowTypes.TOOLWINDOW);
            frmProp.Show(dockPanel1);
        }

        void frmStartPage_onProcessTypeSelected(object sender, EventArgs e)
        {
            StartType type = sender as StartType;
            switch (type.Mode)
            {
                case StartModes.NEW:
                    LoadNewProcessDesigner();
                    break;
                case StartModes.OPEN:
                    LoadProcess();
                    break;
                case StartModes.COPY:
                    MessageBox.Show("Proccess copy");
                    break;
            }
        }

        void frmDoc_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmDocument doc = sender as frmDocument;
            if (!doc.isSaved)
            {
                DialogResult ans = MessageBox.Show(doc.Text.Substring(1) + " is not saved." + Environment.NewLine + "Do you want to save it now?", "EPM Workbench", MessageBoxButtons.YesNoCancel);
                if (ans == System.Windows.Forms.DialogResult.Cancel)
                    e.Cancel = true;
                if (ans == System.Windows.Forms.DialogResult.Yes)
                {
                    if (doc.Filename == null)
                    {
                        saveFileDialog1.ShowDialog();
                        doc.SaveProcess(saveFileDialog1.FileName);
                    }
                    else
                        doc.SaveProcess(doc.Filename);
                }
            }
        }

        private IDockContent GetFromConfigFile(string persist)
        {
            if (persist == typeof(frmProcessFields).ToString())
            {
                frmProcessFields win = new frmProcessFields();
                return win;
            }
            if (persist == typeof(frmProcessStartPage).ToString())
            {
                frmProcessStartPage win = new frmProcessStartPage();
                win.onProcessTypeSelected += new EventHandler(frmStartPage_onProcessTypeSelected);
                return win;
            }
            if (persist == typeof(frmProcessProperty).ToString())
            {
                frmProcessProperty win = new frmProcessProperty();
                return win;
            }
            else
            {
                return null;
            }
        }

        private void frmProcessEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            dockPanel1.SaveAsXml(Workspace.WorkspaceLocation(Workspaces.PROCESS_EDITOR));
        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            LoadNewProcessDesigner();
        }

        private void LoadNewProcessDesigner()
        {
            frmDocument frmDoc = new frmDocument();
            frmDoc.isNew = true;
            frmDoc.Text = string.Format("Process Designer {0}", ProcessNumber++);
            frmDoc.AllowDrop = true;
            frmDoc.ShowHint = DockState.Document;
            frmDoc.DockAreas = Workspace.AllowedDockAreas(WindowTypes.DOCUMENT);
            frmDoc.onFieldSelect += new EventHandler(frmDoc_onFieldSelect);
            frmDoc.FormClosing += new FormClosingEventHandler(frmDoc_FormClosing);
            frmDoc.Show(dockPanel1);
        }

        void frmDoc_onFieldSelect(object sender, EventArgs e)
        {
            foreach (DockContent content in dockPanel1.Contents)
            {
                if (content.GetType() == typeof(frmProcessProperty))
                {
                    frmProcessProperty props = (frmProcessProperty)content;
                    props.SelectedObject = sender;
                }
            }
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            //frmConnectonWizard conWiz = new frmConnectonWizard();
            //conWiz.ShowDialog();
            //MessageBox.Show(conWiz.ConnectionString);
            LoadProcess();
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            SaveProcess();
        }

        private void mnuViewStartPage_Click(object sender, EventArgs e)
        {
            bool startPageFound = false;
            foreach (IDockContent document in dockPanel1.Documents)
            {
                if (document.GetType() == typeof(frmProcessStartPage))
                {
                    document.DockHandler.Activate();
                    startPageFound = true;
                    break;
                }
            }
            if (!startPageFound)
            {
                frmProcessStartPage frmStartPage = new frmProcessStartPage();
                frmStartPage.Text = "Start";
                frmStartPage.AllowDrop = true;
                frmStartPage.ShowHint = DockState.Document;
                frmStartPage.DockAreas = Workspace.AllowedDockAreas(WindowTypes.DOCUMENT);
                frmStartPage.onProcessTypeSelected += new EventHandler(frmStartPage_onProcessTypeSelected);
                frmStartPage.Show(dockPanel1);
            }
        }

        private void saveProcessDefinitionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveProcess();
        }

        private void newProcessDefinitionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadNewProcessDesigner();
        }

        private void openProcessDefinitionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadProcess();
        }

        #region Helper Methods
        private void SaveProcess()
        {
            IDockContent activeDoc = dockPanel1.ActiveDocument;
            if (activeDoc.GetType() == typeof(frmDocument))
            {
                frmDocument doc = (frmDocument)activeDoc;
                if (doc.Filename == null)
                {
                    saveFileDialog1.FileName = doc.ProcessName;
                    if(saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        doc.SaveProcess(saveFileDialog1.FileName);
                }
                else
                    doc.SaveProcess(doc.Filename);
            }
        }

        private void LoadProcess()
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                frmDocument frmDoc = new frmDocument();
                frmDoc.Text = string.Format("Process Designer {0}", ProcessNumber++);
                frmDoc.AllowDrop = true;
                frmDoc.ShowHint = DockState.Document;
                frmDoc.DockAreas = Workspace.AllowedDockAreas(WindowTypes.DOCUMENT);
                frmDoc.onFieldSelect += new EventHandler(frmDoc_onFieldSelect);
                frmDoc.FormClosing += new FormClosingEventHandler(frmDoc_FormClosing);
                frmDoc.isNew = false;
                frmDoc.LoadProcess(openFileDialog1.FileName);
                frmDoc.Show(dockPanel1);
            }
        }
        #endregion

        
    }
}
